<template>
    <el-dialog
            :close-on-click-modal="false"
            :modal="false"
            :visible.sync="visible"
            title="选择发货地址">
        <el-form :model="dataForm" :rules="dataRule" @keyup.enter.native="dataFormSubmit()" label-width="80px"
                 ref="dataForm">
            <el-form-item label="快递公司">
                <el-select placeholder="请选择" v-model="dataForm.dvyId">
                    <el-option
                            :key="item.dvyId"
                            :label="item.dvyName"
                            :value="item.dvyId"
                            v-for="item in dataForm.dvyNames">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="快递单号" prop="dvyFlowId">
                <el-input :min="0" controls-position="right" label="快递单号" v-model="dataForm.dvyFlowId"></el-input>
            </el-form-item>
        </el-form>
        <span class="dialog-footer" slot="footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button @click="dataFormSubmit()" type="primary">确定</el-button>
    </span>
    </el-dialog>
</template>
<script>
    export default {
        data() {
            var validDvyFlowId = (rule, value, callback) => {
                if (!value.trim()) {
                    callback(new Error('不能为空'))
                } else {
                    callback()
                }
            }
            return {
                visible: false,
                dataForm: {
                    dvyId: '',
                    dvyFlowId: 0,
                    dvyNames: [],
                    orderNumber: 0
                },
                dataRule: {
                    dvyFlowId: [
                        {required: true, message: '不能为空', trigger: 'blur'},
                        {validator: validDvyFlowId, trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            init(orderNumber, dvyId, dvyFlowId) {
                this.visible = true
                this.dataForm.orderNumber = orderNumber || ''
                this.dataForm.dvyId = dvyId || ''
                this.dataForm.dvyFlowId = dvyFlowId || ''
                this.$http({
                    url: this.$http.adornUrl('/admin/delivery/list'),
                    method: 'get',
                    params: this.$http.adornParams()
                }).then(({data}) => {
                    this.dataForm.dvyNames = data
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/order/order/delivery`),
                            method: 'put',
                            data: this.$http.adornData({
                                'orderNumber': this.dataForm.orderNumber,
                                'dvyId': this.dataForm.dvyId,
                                'dvyFlowId': this.dataForm.dvyFlowId
                            })
                        }).then(({data}) => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        })
                    }
                })
            }
        }
    }
</script>
